home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / SAT 2.3.7 / Demos / Demo ƒ / StepPlatform Demo ƒ / StepPlatform.c < prev    next >
Encoding:
Text File  |  1995-09-05  |  6.6 KB  |  242 lines  |  [TEXT/KAHL]

  1. // StepPlatform by Nissan Zafrir, based on MyPlatform and StepZkrolly
  2.  
  3. // Modifications by Ingemar R:
  4. // Scroll-length timed after TickCount in order to make it fast enough on slow Macs
  5. // Replaced ox/oy by gSAT.wind.port->portRect.left and .top (ox/oy are obsolete)
  6. // B/W icons and patterns
  7. // Centered window
  8. // Aligned scrolling to multiples of 8 (to account for a limitation in the 1-bit and 4-bit blitters)
  9.  
  10.  
  11. /*
  12. Desirable improvements:
  13. • The man should look better
  14. • Some kind of opponents?
  15. • Ladders?
  16. • Better, more general rect-bounce utilities?
  17. */
  18.  
  19.  
  20.  
  21. //• main;
  22. #include <SAT.h>
  23. #include "myPlatform.h"
  24.  
  25. SATPatHandle    thepat;
  26. SpritePtr        ignoreSp;
  27. Point            p;
  28. SpritePtr        playerSp;
  29. WindowPtr        gWind;
  30. short            gVBLInstalled;
  31. Rect             r;
  32.  
  33.  
  34. enum {
  35.     scrollSizeH = 512,
  36.     scrollSizeV = 384
  37. };
  38.  
  39. Point nowOff = {0,0};
  40.  
  41. #define max(x, y)    (x>y?x:y)
  42. #define min(x, y)    (x>y?y:x)
  43. #define abs(x)        ((x)>0?(x):-(x))
  44.  
  45. pascal Boolean ScrollScreen()
  46. {
  47.     #define    sco    8    //the speed of the scroll, number of pixels per *tick*
  48.  
  49.     long startTicks, frameTime = 1;
  50.  
  51.     Rect srcRect;
  52.     Point    where,were;
  53.  
  54. // If the player sprite is at the border, scroll!
  55.     if ((playerSp->position.h + 64 > scrollSizeH + gSAT.wind.port->portRect.left) ||
  56.         (playerSp->position.h < gSAT.wind.port->portRect.left) || 
  57.         (playerSp->position.v + 64 > scrollSizeV + gSAT.wind.port->portRect.top) ||
  58.         (playerSp->position.v  < gSAT.wind.port->portRect.top) )
  59. //gSAT.wind.port->portRect.left/top = ox/oy!
  60.     {
  61.         were = nowOff;
  62.         where = playerSp->position;
  63.         where.h -= (scrollSizeH >> 1);
  64.         where.v -= (scrollSizeV >> 1);
  65.         if (where.h < 0)
  66.             where.h = 0;
  67.         if (where.v < 0)
  68.             where.v = 0;
  69.         if (where.h + scrollSizeH > gSAT.offSizeH)
  70.             where.h = gSAT.offSizeH - scrollSizeH;
  71.         if (where.v + scrollSizeV > gSAT.offSizeV)
  72.             where.v = gSAT.offSizeV - scrollSizeV;
  73.  
  74.         where.h &= 0xfff8; // Scroll only to multiples of 8, so we won't confuse the 4-bit and 1-bit blitters!
  75.  
  76.         do
  77.         {
  78.             startTicks = TickCount();
  79.  
  80.             if (nowOff.h > where.h) 
  81.                 nowOff.h = max(nowOff.h - sco*frameTime, where.h);
  82.             if (nowOff.h < where.h) 
  83.                 nowOff.h = min(nowOff.h + sco*frameTime, where.h);
  84.             if (nowOff.v > where.v) 
  85.                 nowOff.v = max(nowOff.v - sco*frameTime, where.v);
  86.             if (nowOff.v < where.v) 
  87.                 nowOff.v = min(nowOff.v + sco*frameTime, where.v);
  88.             SATSetPortScreen();
  89.             SetOrigin(nowOff.h, nowOff.v);
  90.             gSAT.wind.bounds = gSAT.wind.port->portRect; // Synch gSAT.wind.bounds with the portRect!
  91.             srcRect = gSAT.wind.port->portRect;
  92.  
  93.             CopyBits(&gSAT.offScreen.port->portBits, &gSAT.wind.port->portBits, &srcRect, &srcRect, srcCopy, nil);
  94.  
  95.             frameTime = TickCount() - startTicks;
  96.         }
  97.         while ((nowOff.h != where.h) || (nowOff.v != where.v));
  98.     }
  99.  
  100. // I tried to add this.
  101. // Shouldn't matter anymore - ox and oy are obsolete /IR
  102. //        gSAT.ox += nowOff.h-were.h;
  103. //        gSAT.oy += nowOff.v-were.v;
  104. } //ScrollScreen
  105.  
  106. void SetupWind()
  107. {
  108.     Rect zr;
  109.     SysEnvRec wrld;
  110.  
  111.     //• Since SAT hasn't been initialized, we can't use gSAT.colorFlag but 
  112.     //• have to check environs ourselves.
  113.     if ( noErr != SysEnvirons(1, &wrld))
  114.         ;//• ignore errors.
  115.  
  116. //    SetRect(&zr, (512-scrollSizeH)/2, 0,  (512-scrollSizeH)/2 + scrollSizeH, 0 + scrollSizeV);
  117. // no- center on screen instead!
  118.     SetRect(&zr, 0, 0, scrollSizeH, scrollSizeV);
  119.     OffsetRect(&zr, (screenBits.bounds.right - screenBits.bounds.left - scrollSizeH) / 2,
  120.                     (screenBits.bounds.bottom - screenBits.bounds.top - scrollSizeV) / 2);
  121.  
  122.     if ( wrld.hasColorQD )
  123.         gWind = NewCWindow(nil, &zr, "\p", false, plainDBox, (WindowPtr)-1L, false, 0);
  124.     else
  125.         gWind = NewWindow(nil, &zr, "\p", false, plainDBox, (WindowPtr)-1L, false, 0);
  126. } //SetupWind
  127.  
  128. Boolean IsOptionPressed()
  129. {
  130.     KeyMap km;
  131.  
  132.     GetKeys(km);
  133.     return (km[1] & 4)!=0;
  134. }
  135.  
  136. IsPressed(k)
  137. unsigned short k;
  138. {
  139.     unsigned char km[16];
  140.     
  141.     GetKeys(km);
  142.     return((km[k>>3]>>(k&7))&1);
  143. }
  144.  
  145. main ()
  146. {
  147.     Rect        tempRect;
  148.     EventRecord    e;
  149.     
  150.     SATInitToolbox();
  151.     GetDateTime(&randSeed);
  152.     
  153.     SATConfigure(true, kVPositionSort, kBackwardCollision, 64);
  154.     SetupWind ();
  155.     
  156.     SetRect(&r, 0, 0, 1000, 484);    //the offscreen size
  157.     SATCustomInit(0, 0, &r, gWind, nil, false, false, false, true, false);
  158.     SATSetSpriteRecSize(sizeof(Sprite));
  159.     
  160.     ShowWindow(gSAT.wind.port);
  161.     SelectWindow(gSAT.wind.port);
  162.     SATHideMBar(gWind);
  163.     
  164.     /* fill the backscreen in a pattren */
  165.     SATSetPortBackScreen(); 
  166.     if (IsOptionPressed())
  167.         thepat = SATGetPat(SATRand(5)+128);    // choose a Pat in Random
  168.     else
  169.         thepat = SATGetPat(128);//128-brown wall, 130-gray wall pattren
  170.     SATPenPat(thepat);
  171.     SetRect(&tempRect, 0, 0, gSAT.offSizeH, gSAT.offSizeV+100);
  172.     PaintRect(&tempRect);
  173.     CopyBits(&(gSAT.backScreen.port)->portBits, &gSAT.offScreen.port->portBits, &gSAT.offScreen.port->portRect, &gSAT.offScreen.port->portRect, srcCopy, nil);
  174.     SATBackChanged(&gSAT.offScreen.port->portRect);
  175.     SATRedraw();
  176.     
  177.     /*Initialize all sprite units*/
  178.     InitPlayerSprite();
  179.     InitPlatform();
  180.     InitEmptyPlatform();
  181.     InitMovPlatform();
  182.     InitHMovPlatform();
  183.     InitInformationArea();
  184.     
  185.     SATRedraw();
  186.     
  187.     /* SetUp my Sprites    */
  188.     GetMouse(&p);
  189.     playerSp = SATNewSprite(1, p.h, p.v, &SetupPlayerSprite);    // Keep Player Sprite
  190.     ignoreSp = SATNewSprite(0, 0, 350, &SetupEmptyPlatform);    // the Floor Platform
  191.         SetRect(&ignoreSp->hotRect,0,0,gSAT.offSizeH-150,20);
  192.     ignoreSp = SATNewSprite(0, 55, 300, &SetupPlatform);        // Standing Platform
  193.     ignoreSp = SATNewSprite(0, 245, 235, &SetupPlatform);        // Standing Platform
  194.     ignoreSp = SATNewSprite(55, 355, 200, &SetupMovPlatform);    //50=MinV    200=MaxV
  195.         //ignoreSp->position.h=30;    // I can change the defeults
  196.     ignoreSp = SATNewSprite(100, 55, 200, &SetupMovPlatform);    //100=MinV    230=MaxV
  197.     ignoreSp = SATNewSprite(128, 270, 120, &SetupHMovPlatform);    //120=MinH   150=MaxH
  198.         ignoreSp->speed.h = 2;
  199.     
  200.     ignoreSp = SATNewSprite(250, 850, 400, &SetupMovPlatform);    //50=MinV    200=MaxV
  201.  
  202.     ignoreSp = SATNewSprite(55, 700, 250, &SetupMovPlatform);    //50=MinV    200=MaxV
  203.     ignoreSp = SATNewSprite(430, 630, 85, &SetupHMovPlatform);    //50=MinV    200=MaxV
  204.  
  205.     /*Update the game window once more, so the pattern and what we drawn in DrawInfo are shown. */
  206.     SATRedraw();
  207.  
  208.     HideCursor();
  209.     SATSoundOff();
  210.  
  211.     if (gSAT.colorFlag) // Can't do SlotVInstall without CQD
  212.         if (IsOptionPressed())    {
  213.             InstallVBLCounter();
  214.             SATInstallSynch(&VBLCounterProc);    //WaitForSync
  215.             SysBeep(7);
  216.             gVBLInstalled = true;
  217.         }
  218.     
  219. /* the main loop */
  220.  
  221.     gSAT.wind.bounds = (*gSAT.wind.port).portRect;
  222.     SATRedraw();
  223.     SATSetPortScreen();
  224.     do
  225.     {
  226.         SATRun(true);    //!IsOptionPressed()
  227.         ScrollScreen();
  228.         if (IsOptionPressed())
  229.             DrawProgrammerInfo();
  230.                         //if (IsOptionPressed()) if (WaitNextEvent ( everyEvent, &e, 10, nil )) ;
  231.     } while (! Button ());
  232.  
  233. /* cleanning up */
  234.     SATSetPortScreen();
  235.     if (gVBLInstalled)
  236.         RemoveVBLCounter();
  237.     SATSoundShutup();
  238.     
  239.     ShowCursor();
  240.     FlushEvents(everyEvent, 0);
  241. } //main
  242.